/**
* @file PySpp.cpp in RealTimeSpp
* @author rebeater
* @comment Python interface for SPP
* Create on 11/1/21 9:52 AM
* @version 1.0
**/
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "rt_spp.h"
namespace py = pybind11;
RealTimeSpp *spp;

void Initialize() {
  if (!spp) {
	spp = new RealTimeSpp();
  }
}
bool LoadNavFromRnx(string &s) {
  if(!spp) {
    std::cout << "run PySpp.Initialize() First\n";
    return false;
  }
  return spp->LoadNavFromRnx(s);
}

bool Update(int week, int second, char *s) {
  if(!spp) {
    std::cout << "run PySpp.Initialize() First\n";
    return false;
  }
  return spp->Update(week, second, s);
}

py::object GetPosition() {
  if(!spp) {
    std::cout << "run PySpp.Initialize() First\n";
    return py::none();
  }
  Position pos{0,0,0,0,0,0,0};
  if (spp->GetSppPosition(&pos)) {
	return  py::cast(pos);
  } else {
    return  py::none();
//	return {0, 0, 0, 0, 0};
  }
}

void Close() {
  if (spp) {
	delete spp;
	spp = nullptr;
  }
}

PYBIND11_MODULE(PySpp, m) {
  m.doc() = "Single Point Position Program based on rtklib"; //
  m.def("Initialize", &Initialize, "A function which initializes the object PySpp");
  m.def("LoadNavFromRnx", &LoadNavFromRnx, "A function which reads Navigation file from rinex file");
  m.def("Update", &Update, "A function which update a satellite information");
  m.def("GetPosition", &GetPosition, "A function which runs single point position and return the result");
  m.def("Close", &Close, "A function which delete the object PySpp");

  pybind11::class_<Position>(m, "Position")
	  .def_readwrite("lat", &Position::lat)
	  .def_readwrite("lon", &Position::lon)
	  .def_readwrite("q", &Position::q)
	  .def_readwrite("ns", &Position::ns)
	  .def_readwrite("height", &Position::height)
	  .def_property_readonly("std",[](const Position &p){
	    return std::vector<double>{p.std[0],p.std[1],p.std[2]};
	  });

	  /*.def("std", [](const Position &p, int index) {
		return p.std[index];
	  });*/
}



